$(function() {

    'use strict';

    var imgLoader = {

        _srcs      : [],
        _onLoaded  : null,
        _onLoading : null,

        load: function() {

            var srcs      = this._srcs,
                l         = srcs.length,
                loadCount = 0,
                that      = this,
                tmp,
                i;

            function onImgLoad() {
                if (++loadCount === l) {
                    that._onLoaded && that._onLoaded();
                } else {
                    that._onLoading && that._onLoading(Math.floor(loadCount / l * 100));
                }
            }

            if (!l) {
                that._onLoaded && that._onLoaded();
                return this;
            }

            for (i = l; i--;) {
                tmp        = new Image();
                tmp.src    = srcs[i];
                tmp.onload = onImgLoad;
            }

            return this;
        },

        init: function(arrSrcs, onLoaded, onLoading) {

            this._srcs      = arrSrcs.slice(0);
            this._onLoaded  = onLoaded;
            this._onLoading = onLoading;

            return this;
        }
    };

    

    // Init
    var loadList = [],
        list = $('#preload_list img');

    for (var i = list.length; i--;) {
        loadList.push(list.eq(i).data('src'));
    }

    imgLoader.init(loadList, function() {

        $('#loading').fadeOut(2000, function() {
            $('#loading').remove();
        });

    }, function(pct) {
        $('#loading .tip span').html(pct);
    }).load();

    indexPage.init();

});

var indexPage = (function(){
    var _fn = {
        _join:function(){
            $(".page").hide();
            $(".page-explore").show();
            setTimeout(function(){
                $(".page-explore .ship").addClass("active");
            },700);
        }
        //参加探索活动，调用接口
        ,explore:function(){

            var index = Math.floor(Math.random() * 10000 % 3),
                cnames = ['.page-code', '.page-card', '.page-fail'],
                wrapnames = ['.code-wrap', ".card-wrap", ".fail-wrap"],
                topOffset =[290,290,260];

            $(".page").hide();


            $(cnames[index]).show();
            var offsetTop = $(wrapnames[index]).offset().top;
            console.log(offsetTop);
            offsetTop +=topOffset[index];
            $(".qrcode").css({
                top:(1)*offsetTop
            }).show();
        }
        ,playVideo:function(){

        }
        ,eventBind:function(){
            Common.bindShowRule();
            Common.bindShowReward();
            Common.bindSharePrompt();

            $(".join-btn").bind("click",_fn._join);
            $('.explore-btn').click(_fn.explore);

            $(".about-nav").bind("click",function(){
                var videoUrl ="http://videogame.ztgame.com.cn/wildstar/20150812/whatiswildstar.mp4";
                video.open(videoUrl);
            })
        }
        ,init:function(){
            _fn.eventBind();
            video.init();
        }
    };

    return {
        init:_fn.init
    }
})();